package com.daffodil.workable.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.daffodil.core.dao.JpaDao;
import com.daffodil.core.entity.Query;
import com.daffodil.workable.entity.WorkableResult;
import com.daffodil.workable.service.IWorkableResultService;
import com.daffodil.util.HqlUtils;

/**
 * -节点执行结果Service接口业务实现层
 * @author yweijian
 * @date 2022-04-07
 * @version 1.0
 * @description
 */
@Service
public class WorkableResultServiceImpl implements IWorkableResultService {

    @Autowired
    private JpaDao<String> jpaDao;

    @Override
    public List<WorkableResult> selectWorkableResultList(Query<WorkableResult> query) {
        StringBuffer hql = new StringBuffer("select new WorkableResult(r.id, r.traceId, r.taskId, t.taskName, r.nodeId, n.name, r.status, r.message, r.createTime) from WorkableResult r ");
        hql.append("left join WorkableNode n on r.nodeId = n.nodeId and r.taskId = n.taskId ");
        hql.append("left join WorkableTask t on r.taskId = t.id ");
        hql.append("where 1=1 ");
        List<Object> paras = new ArrayList<Object>();
        HqlUtils.createHql(hql, paras, query, "r");
        return jpaDao.search(hql.toString(), paras, WorkableResult.class, query.getPage());
    }

    @Override
    public WorkableResult selectWorkableResultById(String id) {
        StringBuffer hql = new StringBuffer("select new WorkableResult(r.id, r.traceId, r.taskId, t.taskName, r.nodeId, n.name, r.status, r.message, r.createTime, r.content) from WorkableResult r ");
        hql.append("left join WorkableNode n on r.nodeId = n.nodeId and r.taskId = n.taskId ");
        hql.append("left join WorkableTask t on r.taskId = t.id ");
        hql.append("where r.id = ? ");
        return jpaDao.find(hql.toString(), id, WorkableResult.class);
    }

    @Override
    @Transactional
    public void insertWorkableResult(WorkableResult workableResult) {
        jpaDao.save(workableResult);
    }

    @Override
    @Transactional
    public void updateWorkableResult(WorkableResult workableResult) {
        jpaDao.update(workableResult);
    }

    @Override
    @Transactional
    public void deleteWorkableResultByIds(String[] ids) {
        jpaDao.delete(WorkableResult.class, ids);
    }

    @Override
    public WorkableResult selectWorkableResult(WorkableResult result) {
        StringBuffer hql = new StringBuffer("select new WorkableResult(r.id, r.traceId, r.taskId, t.taskName, r.nodeId, n.name, r.status, r.message, r.createTime, r.content) from WorkableResult r ");
        hql.append("left join WorkableNode n on r.nodeId = n.nodeId and r.taskId = n.taskId ");
        hql.append("left join WorkableTask t on r.taskId = t.id ");
        hql.append("where r.taskId = ? and r.nodeId = ? and r.traceId = ? ");
        List<Object> paras = new ArrayList<Object>();
        paras.add(result.getTaskId());
        paras.add(result.getNodeId());
        paras.add(result.getTraceId());
        return jpaDao.find(hql.toString(), paras, WorkableResult.class);
    }

}
